package com.qingrongby.shop.validators;

import org.apache.commons.lang.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @CLASSNAME TokenPasswordValidator
 * @DESCRIPTION TODO
 * @AUTHOR hzp
 * @DATE 2020/7/7
 * @VERSION 1.0
 **/
public class TokenPasswordValidator implements ConstraintValidator<TokenPassword,String> {

    private Integer min;

    private Integer max;

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isBlank(s)){
            return true;
        }
        if(s.length()<this.min){
            return false;
        }
        if (s.length()>this.max){
            return false;
        }
        return true;
    }

    @Override
    public void initialize(TokenPassword constraintAnnotation) {
        this.max=constraintAnnotation.max();
        this.min=constraintAnnotation.min();
    }
}
